package com.dsideal.cloud.cas.client.cas.service;


import com.dsideal.cloud.cas.client.cas.entity.CasUserDetails;
import com.dsideal.cloud.cas.client.config.MysqlConfig;
import com.dsideal.cloud.common.handler.BaseContextHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.cas.authentication.CasAssertionAuthenticationToken;
import org.springframework.security.core.userdetails.AuthenticationUserDetailsService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
*
 * @author suny
*/


@Service
public class CasAuthenticationUserDetailsService
        implements AuthenticationUserDetailsService<CasAssertionAuthenticationToken> {

    @Autowired
    private MysqlConfig mysqlConfig;
    @Override
    public UserDetails loadUserDetails(CasAssertionAuthenticationToken token) throws UsernameNotFoundException {
        // 根据用户名查询用户信息 用户信息都一样故只拿第一条
            Map<String,Object> map= mysqlConfig.queryByname(token.getName()).get(0);
            if(!map.isEmpty()){
                CasUserDetails casUserDetails =new CasUserDetails();
//                casUserDetails.setName(map.get("name").toString());
                //登录名
                casUserDetails.setName(String.valueOf(map.get("login_name")));
                //主键id
                casUserDetails.setId(String.valueOf(map.get("base_user_id").toString()));
                //部门id
                casUserDetails.setBureauId(String.valueOf(map.get("bureau_id").toString()));
                //用户名
                casUserDetails.setUsername(String.valueOf(map.get("user_name").toString()));
                //密码
                casUserDetails.setPassword(String.valueOf(map.get("login_password").toString()));
                return  casUserDetails;
            }

        return null;
    }


}
